package com.lry.basic.design_mode.observer.demo1;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/11/13
 */
public class Subject {
    private int state;//被观察者状态变量
    private List<Observer> observers = new ArrayList<Observer>();
    public int getState() {
        return state;
    }
    public void notifyAllObservers(){
        for (Observer observer : observers) {
            observer.update();
        }
    }
    public void setState(int state) {
        this.state = state;
        notifyAllObservers();
    }

    public void attach(Observer observer){
        observers.add(observer);
    }
}
